package com.weizhu.swufer.core.filter;

import com.weizhu.swufer.core.client.Invoker;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.rpc.RpcRequest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FilterChain {

    private List<Filter> filters = new ArrayList<>();

    private int index;

    public FilterChain(){
        this.index = 0;
    }

    public void addAll(List<Filter> filters){
        this.filters.addAll(filters);
    }

    public void addFilter(Filter filter){
        this.filters.add(filter);
    }

    public Object doFilter(InvokeParam invokeParam) throws Exception {
        if (index < filters.size()){
            return filters.get(index++).doFilter(invokeParam,this);
        }else{
            return invokeParam;
        }
    }

    public void removeFilter(Class<?> filterClass){
        filters.removeIf(filter -> filter.getClass().equals(filterClass));
    }
}
